<template>
  <div class="login">
    <van-nav-bar title="会员登录" left-arrow @click-left="$router.go(-1)"/>
    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input class="inp" maxlength="11" v-model="phone" placeholder="请输入手机号码" type="text">
        </div>
        <div class="form-item">
          <input class="inp" maxlength="5" v-model="captchaCode" placeholder="请输入图形验证码" type="text">
          <img :src="imgUrl" alt="" @click="changeImgCode">
        </div>
        <div class="form-item">
          <input class="inp" placeholder="请输入短信验证码" type="text" v-model="smsCode">
          <button @click="getShortMessageCode" v-if="isGet">获取验证码</button>
          <button v-else style="color: #ecb53c">重新发送:{{ second }}</button>
        </div>
      </div>
      <div class="login-btn" @click="login">登录</div>
      <div class="login-btn" @click="loginOut">退出登录</div>
    </div>
  </div>
</template>

<script>
import { getImageCode, getShortMessageCode, getTokenAndLogin } from '@/api/allAxios'
import { mapMutations } from 'vuex'
import { throttle } from 'lodash'

export default {
  name: 'LoginPage',
  created () {
    this.fetchImageCode()
  },
  data () {
    return {
      imgUrl: '',
      key: '',
      phone: '',
      captchaCode: '',
      smsCode: '',
      isGet: true,
      second: 5,
      totalSecond: 5
    }
  },
  methods: {
    ...mapMutations('user', ['setUserInfo', 'userLogout']),
    async getShortMessageCode () {
      const {
        message
      } = await getShortMessageCode(this.captchaCode, this.key, this.phone)
      this.isGet = !this.isGet
      if (this.second > 0) {
        this.isGet = false
        const timer = setInterval(() => {
          this.second--
          if (this.second === 0) {
            clearInterval(timer)
            this.second = this.totalSecond
            this.isGet = true
          }
        }, 1000)
      }
      console.log(message)
    },
    async login () {
      if (!this.validatePhone(this.phone)) {
        this.$toast('请输入正确的手机号', { duration: 2000 })
        return
      }
      if (!this.validateCaptcha(this.captchaCode)) {
        this.$toast('请输入正确的你的🐎', { duration: 2000 })
        return
      }
      const {
        data: {
          token,
          userId
        }
      } = await getTokenAndLogin(this.phone, this.smsCode)
      console.log('登录陈宫：token=', token, 'userId=', userId)
      // this.$store.commit('user/setUserInfo', {
      //   token,
      //   userId
      // })
      this.setUserInfo({
        token,
        userId
      })
      if (token) {
        await this.$router.push('/')
      }
      // this.$router.push('/')
      // const response = await testGetTokenAndLogin()
    },
    loginOut () {
      this.userLogout()
      this.$router.push('/')
    },
    // 获取图片验证码
    async fetchImageCode () {
      const {
        data: {
          key,
          base64
        }
      } = await getImageCode()
      this.imgUrl = base64
      this.key = key
    },
    // 节流处理的 changeImgCode 方法
    changeImgCode: throttle(async function () {
      await this.fetchImageCode() // 重新获取验证码
    }, 5000), // 2秒间隔
    validatePhone (phone) {
      const phoneRegex = /^[1][3-9][0-9]{9}$/ // 正则表达式验证手机号
      return phoneRegex.test(phone)
    },

    validateCaptcha (captcha) {
      return captcha.length === 4 // 验证验证码是否为四位
    }
  }

}
</script>

<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;

    h3 {
      font-size: 26px;
      font-weight: normal;
    }

    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;

    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }

    img {
      width: 94px;
      height: 31px;
    }

    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg, #ecb53c, #ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0, 0, 0, .1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
